package com.example.demo.dao;

import com.example.demo.bean.Word;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author Jim Chen
 * @date 2021-04-15
 */
@Mapper
@Repository
public interface WordDao {

    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "type", column = "type"),
            @Result(property = "catalog", column = "catalog"),
            @Result(property = "englishAbbr", column = "englishAbbr"),
            @Result(property = "englishName", column = "englishName"),
            @Result(property = "chineseName", column = "chineseName")
    })
    @Select("select * from word")
    List<Word> findAll();

    @Insert({
            "<script>",
            "insert into word(type, catalog, englishAbbr, englishName, chineseName) values ",
            "<foreach collection='words' item='item' index='index' separator=','>",
            "(#{item.type}, #{item.catalog}, #{item.englishAbbr},#{item.englishName},#{item.chineseName})",
            "</foreach>",
            "</script>"
    })
    int insertWordList(@Param(value = "words") List<Word> words);

    @Delete("delete from word")
    int deleteAll();


    @Select("select * from word where id = #{id}")
    Word getById(Integer id);

    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "type", column = "type"),
            @Result(property = "catalog", column = "catalog"),
            @Result(property = "englishAbbr", column = "englishAbbr"),
            @Result(property = "englishName", column = "englishName"),
            @Result(property = "chineseName", column = "chineseName")
    })
    @Select("select * from word where englishName = #{englishName}")
    List<Word> findEnglishAbbrByEnglishName(String englishName);

    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "type", column = "type"),
            @Result(property = "catalog", column = "catalog"),
            @Result(property = "englishAbbr", column = "englishAbbr"),
            @Result(property = "englishName", column = "englishName"),
            @Result(property = "chineseName", column = "chineseName")
    })
    @Select("select * from word where englishAbbr = #{englishAbbr}")
    List<Word> findEnglishNameByEnglishAbbr(String englishAbbr);

    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "type", column = "type"),
            @Result(property = "catalog", column = "catalog"),
            @Result(property = "englishAbbr", column = "englishAbbr"),
            @Result(property = "englishName", column = "englishName"),
            @Result(property = "chineseName", column = "chineseName")
    })
    @Select("select * from word where chineseName like CONCAT('%',#{chineseName},'%')")
    List<Word> findEnglishNameByChineseName(String chineseName);

}
